package com.o.mj.controller;

import com.github.pagehelper.PageInfo;
import com.o.mj.dao.StudyHomeMapper;
import com.o.mj.dao.WorkPersonMapper;
import com.o.mj.pojo.DataColleges;
import com.o.mj.pojo.Login;
import com.o.mj.pojo.UserInfo;
import com.o.mj.service.PersonService;
import com.o.mj.service.StudyHomeService;
import com.o.mj.service.WorkPersonService;
import com.o.mj.util.exception.R;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * 学院统计层
 */
@Controller
public class StudyHomeController {
    @Autowired
    PersonService personS;

    @Autowired
    WorkPersonService workPS;

    @Autowired
    StudyHomeService studyHomeService;

    @Autowired
    StudyHomeMapper studyHomeMapper;

    /**
     * 数学
     */
    @GetMapping("/docM")
    public String docM(Model model, HttpSession session, HttpServletRequest request, @RequestParam(name = "pageNum", defaultValue = "1") int pageNum) {
        try {
            Object userId = session.getAttribute("userId");
            String srcBase = personS.getImgFileToBase64((String) userId);
            model.addAttribute("srcBase", srcBase);
            UserInfo usi = workPS.getPersonMessage((String) userId);
            model.addAttribute("aiName", usi.getAiName());
        } catch (Exception e) {
        }
        PageInfo<UserInfo> pageInfo = studyHomeService.selectAll(pageNum, 10);
        request.setAttribute("pageInfo", pageInfo);
//        System.out.println("当前页：" + pageInfo.getPageNum());
//        System.out.println("每页显示的数量：" + pageInfo.getPageSize());
//        System.out.println("当前页的数量：" + pageInfo.getSize());
//        System.out.println("总记录数：" + pageInfo.getTotal());
//        System.out.println("总页数：" + pageInfo.getPages());
//        System.out.println("结果集:" + pageInfo.getList());
//        System.out.println("第一页:" + pageInfo.getNavigateFirstPage());
//        System.out.println("前一页:" + pageInfo.getPrePage());
//        System.out.println("下一页:" + pageInfo.getNextPage());
//        System.out.println("最后一页:" + pageInfo.getNavigateLastPage());
//        System.out.println("是否为第一页：" + pageInfo.isIsFirstPage());
//        System.out.println("是否为最后一页：" + pageInfo.isIsLastPage());
//        System.out.println("是否有前一页：" + pageInfo.isHasPreviousPage());
//        System.out.println("是否有下一页：" + pageInfo.isHasNextPage());
//        System.out.println("导航页码数" + pageInfo.getNavigatePages());
//        System.out.println("所有导航页号" + Arrays.toString(pageInfo.getNavigatepageNums()));
        request.setAttribute("pageInfo", pageInfo);
        return "pages_docM";
    }

    @RequestMapping("/mCWork")
    @ResponseBody
    public List mCWork() {
        List list = new ArrayList<>();
        List<String> pc = new ArrayList<>();

        List<UserInfo> workLose = studyHomeMapper.getMatch();
        List<UserInfo> worker = studyHomeMapper.getMatchI();
        Map<String, List> mathChat = new HashMap();
        Map<String, String> workLoseChat = new HashMap();
        // 获取数组中的班级项
        for (int i = 0; i < workLose.size(); i++) {
            String personCL = workLose.get(i).getPersonClass();
            pc.add(personCL);
        }
        //便利数组去重
        List result = new ArrayList();
        for (String PAC : pc) {
            if (!result.contains(PAC)) {
                result.add(PAC);
            }
        }
        //获得一共有多少班级数
        pc.clear();
        pc.addAll(result);

        //迭代遍历排序
        for (int i = 0; i < pc.size() - 1; i++) {
            for (int j = 0; j < pc.size() - i - 1; j++) {
                if (pc.get(j).compareTo(pc.get(j + 1)) > 0) {
                    String a = pc.get(j);
                    pc.set(j, pc.get(j + 1));
                    pc.set(j + 1, a);
                }
            }
        }
        //遍历数组将对应的值放进Map中去
        for (int i = 0; i < workLose.size(); i++) {
            if (!result.contains(workLose.get(i).getPersonClass())) {
                String personCL = workLose.get(i).getPersonClass();
                String personLose = workLose.get(i).getId();
                workLoseChat.keySet().add(personCL);
                workLoseChat.values().add(personLose);
            }
        }


        for (int i = 0; i < worker.size(); i++) {
            worker.get(i).getPersonClass();
        }


//        list.add(personClass);
        list.add(workLose);
        list.add(worker);
        return list;
    }
}
